package com.lee.learn.android.app.learnandroid;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


/**
 * 添加第二个Activity
 * Created by alex on 14-3-10.
 */
public class SecondActivity extends Activity implements
        NavigationDrawerFragment.NavigationDrawerCallbacks,
        ActionBar.TabListener ,
        ViewPager.OnPageChangeListener{

    private NavigationDrawerFragment mNavigationDrawerFragment;

    private CharSequence mTitle;

    private ActionBar mActionBar;

    private ViewPager mViewPager;

    private SectionsPagerAdapter mSectionsPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getFragmentManager().findFragmentById(R.id.navigation_drawer);

        mTitle = getTitle();

        /**
         * 设置导航抽屉
         */
        mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));

        mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.my_pager);

        if (mViewPager != null){
            mViewPager.setAdapter(mSectionsPagerAdapter);
            mViewPager.setOnPageChangeListener(this);

            mActionBar = getActionBar();
            mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
                mActionBar.addTab(
                        mActionBar.newTab()
                            .setText(mSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this)
                );
            }

        }

    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        if (position == 3) {
            return;
        }

        Intent startIntent = new Intent(this, PassingDataActivity.class);
        startActivity(startIntent);
    }




    public static class TabFragment extends Fragment {

        private static final String ARG_SECTION_NUMBER = "section_number";

        public static TabFragment newInstance(int sectionNumber) {
            TabFragment fragment = new TabFragment();

            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_tab,container,false);
            TextView label = (TextView) rootView.findViewById(R.id.tab_label);
            label.setText("Tab "+ Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }
    }


    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onPageSelected(int position) {
        getActionBar().setSelectedNavigationItem(position);
    }



    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
